﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using BikeManageBusiness.logical;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    public partial class AddUser : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IAccount _account = Kernel.Get<IAccount>();

        public AddUser()
        {
            InitializeComponent();

            try
            {
                cmbGender.SelectedIndex = 0;

                cmbNation.DataSource = new List<string>() { "汉", "蒙古", "回", "藏", "维吾尔", "苗", "彝", "壮", "布依", "朝鲜", "满", "侗", "瑶", "白", "土家", "哈尼", "哈萨克", "傣", "黎", "僳僳", "佤", "畲", "高山", "拉祜", "水", "东乡", "纳西", "景颇", "柯尔克孜", "土", "达斡尔", "仫佬", "羌", "布朗", "撒拉", "毛南", "仡佬", "锡伯", "阿昌", "普米", "塔吉克", "怒", "乌孜别克", "俄罗斯", "鄂温克", "德昂", "保安", "裕固", "京", "塔塔尔", "独龙", "鄂伦春", "赫哲", "门巴", "珞巴", "基诺族" };
                cmbNation.SelectedIndex = 0;
            }
            catch (Exception)
            {
                
            }
        }

        /// <summary>
        ///     读身份证信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveIDInfo_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                GC.Collect();
                var dkqa16DLogical = new Dkqa16dLogical();
                ResultInfo<IDCardData> idCardData = dkqa16DLogical.GetIdCardData();

                //idCardData.ResultNumber = ResultEnum.Success;
                //idCardData.Obj = new IDCardData { Address = "长沙", Born = "19890110", GrantDept = "澧县公安局", IDCardNo = "323232323232323232", Name = "向钊豫", Nation = "china", PhotoFileName = @"C:\Users\xiangzy.CHANGSHA\Documents\Visual Studio 2010\Projects\BikeManage\WinForm\bin\Debug\070AE854D8D44577B75E14E88629B90C.jpg", reserved = "", Sex = "男", UserLifeBegin = "20101010", UserLifeEnd = "20201010" };

                if (idCardData.ResultNumber == ResultEnum.Success)
                {
                    //身份证信息赋值
                    txtName.Text = idCardData.Obj.Name;
                    txtGender.Text = idCardData.Obj.Sex;
                    cmbGender.Text = idCardData.Obj.Sex;
                    txtNation.Text = idCardData.Obj.Nation;
                    cmbNation.Text = idCardData.Obj.Nation;
                    txtHomeAddress.Text = idCardData.Obj.Address;
                    try
                    {
                        String dtBorn = idCardData.Obj.Born.Substring(0, 4) + "/" + idCardData.Obj.Born.Substring(4, 2) + "/" + idCardData.Obj.Born.Substring(6, 2);    
                        dateBorn.Value = Convert.ToDateTime(dtBorn);
						//dateBorn.Value = Convert.ToDateTime(idCardData.Obj.Born);
                    }
                    catch (Exception)
                    {
                        dateBorn.Value = DateTime.Now;
                    }
                    txtIDNoR.Text = idCardData.Obj.IDCardNo;
                    txtIdGov.Text = idCardData.Obj.GrantDept;
                    userPicture.ImageLocation = idCardData.Obj.PhotoFileName;
                    //显示填用户信息界面
                    tabReadCard.Visible = false;
                    tabIdInfoShow.Visible = true;
                    //tabAddUser.SelectedTab = tabIdInfoShow;
                }
                else
                {
                    errorMessage = idCardData.Describe;
                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        MyMessageBox.ShowMessageBoxError(errorMessage);
                    }
                    //DialogResult lDR = MessageBox.Show("无法进行自动身份信息阅读，准备手工录入信息吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    DialogResult lDR = MyMessageBox.ShowMessageBoxQuestion("无法进行自动身份信息阅读，准备手工录入信息吗？");
                    if (DialogResult.Yes == lDR)
                    {
                        tabReadCard.Visible = false;
                        tabIdInfoShow.Visible = true;
                        txtName.Enabled = true;   txtName.ReadOnly = false;
                        dateBorn.Enabled = true; 
                        txtNation.Enabled = true; txtNation.ReadOnly = false;
                        txtGender.Enabled = true; txtGender.ReadOnly = false;
                        txtIDNoR.Enabled = true; txtIDNoR.ReadOnly = false;
                        txtIdGov.Enabled = true; txtIdGov.ReadOnly = false;
                        txtHomeAddress.Enabled = true; txtHomeAddress.ReadOnly = false;
                    } 
                }
            }
            catch (Exception e1)
            {
                errorMessage = e1.Message;
				if (!string.IsNullOrEmpty(errorMessage))
	            {
	                MyMessageBox.ShowMessageBoxError(errorMessage);
	            }
            }        
            GC.Collect();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            var zq58Logical = new Zq58Logical();
            ResultInfo<Decimal> initialMoney = zq58Logical.QueryPurse();
            if (initialMoney.ResultNumber == ResultEnum.Success)
            {
                lbInitialMoney.Text = initialMoney.Obj.ToString();
            }
            else
            {
                errorMessage = initialMoney.Describe;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
            tabReadUserInfo.Visible = true;
            tabSaveOver.Visible = false;
        }

        /// <summary>
        ///     显示读出来的身份证信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIdUp_Click(object sender, EventArgs e)
        {
            tabReadCard.Visible = true;
            tabIdInfoShow.Visible = false;
            //tabAddUser.SelectedTab = tabReadCard;
        }

        /// <summary>
        ///     显示读cpu卡的提示信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIdNext_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtName.Text))
            {
                MyMessageBox.ShowMessageBoxInfo("请输入用户名，然后点击下一步");
            }else
            {
                tabIdInfoShow.Visible = false;
                tabReadCpu.Visible = true;
                
            }
        }

        /// <summary>
        ///     回到身份证信息展示页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadCpuUp_Click(object sender, EventArgs e)
        {
            tabIdInfoShow.Visible = true;
            tabReadCpu.Visible = false;
        }

        /// <summary>
        ///     测试cpu卡是否放好，显示读cpu卡的提示页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadCpuNext_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            var zq58Logical = new Zq58Logical();
            // 读取用户卡中的数据
            try
            {
                ResultInfo<CpuAccount> cpuAccount = new ResultInfo<CpuAccount>();
                Zq58Logical ZQ58 = new Zq58Logical();
                cpuAccount = ZQ58.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    if ("1.01C" == cpuAccount.Obj.CpuCardVersion)
                    {
                        if ((cpuAccount.Obj.DisplayName != "") || (System.Convert.ToDecimal(cpuAccount.Obj.Fee) > 0))
                        {
                            DialogResult lDR = MyMessageBox.ShowMessageBoxQuestion(string.Format("非空白卡片，原持有人：{0} \r\n为避免误操作，建议更换卡片再继续！点“是”强制覆盖（测试期间可强制覆盖，正式版不允许覆盖)？", Converts.Replace0Str(cpuAccount.Obj.DisplayName)));//MessageBox.Show("非空白卡片，原持有人：" + cpuAccount.Obj.DisplayName + "\r\n为避免误操作，建议更换卡片再继续！点“是”强制覆盖（测试期间可强制覆盖，正式版不允许覆盖)？", "强制覆盖？", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                            if (lDR != DialogResult.Yes)   return;
                        }
                    }
                    else if( 0 != cpuAccount.Obj.baCardVer[0] || 0 != cpuAccount.Obj.baCardVer[1] || 0 != cpuAccount.Obj.baCardVer[2] || 0 != cpuAccount.Obj.baCardVer[3] || 0 != cpuAccount.Obj.baCardVer[4])
                    {
                        MyMessageBox.ShowMessageBoxError("不是空白卡片且不是本系统发行，为避免误操作，请更换卡片！");
                        //MessageBox.Show("不是空白卡片且不是本系统发行，为避免误操作，请更换卡片！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error););
                        return;
                    }
                    /*ResultInfo<BorrowBike> borrowBike = ZQ58.ReadBorrowBikeInfo();
                    if (borrowBike.ResultNumber == ResultEnum.Success)
                    {
                        ResultInfo<Decimal> totalMoney = zq58Logical.QueryPurse();
                        if (totalMoney.ResultNumber == ResultEnum.Success)
                        {
                            //初始化用户信息显示
                            lbName.Text = cpuAccount.Obj.DisplayName;
                            lbSex.Text = cpuAccount.Obj.Sex;
                            lbNation.Text = cpuAccount.Obj.Nation;
                            lbValideDate.Text = borrowBike.Obj.EndAccountDate;
                            lbAddress.Text = cpuAccount.Obj.Address;
                            lbCpuVersion.Text = cpuAccount.Obj.CpuCardVersion;
                            lbCpuNo.Text = cpuAccount.Obj.CpuCardNo;
                            lbStatus.Text = cpuAccount.Obj.CpuCardStatus;
                            lbCpuType.Text = cpuAccount.Obj.CpuCardType;
                            lbFee.Text = cpuAccount.Obj.Fee;
                            lbIdGov.Text = cpuAccount.Obj.Address;
                            lbIdNo.Text = cpuAccount.Obj.IdCardNo;
                            lbIdType.Text = cpuAccount.Obj.IdCardType;
                            lbMobile.Text = cpuAccount.Obj.Mobile;
                            lbTel.Text = cpuAccount.Obj.Phone;*/
                }
                else
                {
                    MyMessageBox.ShowMessageBoxError("无卡片或未检测到卡片，请正放好卡片，然后再继续！");
                    //MessageBox.Show("无卡片或未检测到卡片，请正放好卡片，然后再继续！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error););
                    return;
                }

                //是否清空当前卡用户的余额
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<decimal> currentMon = zq58Logical.QueryPurse();
                    if ((currentMon.ResultNumber == ResultEnum.Success) && (currentMon.Obj > 0))
                    {
                        string desc = string.Format("当前用户卡的余额不为空，当前余额是：{0}，是否清空？", currentMon.Obj.ToString());
                        if (MyMessageBox.ShowMessageBoxQuestion(desc) == DialogResult.Yes)
                        {
                            ResultInfo<byte[]> cosm = zq58Logical.CostMoney(currentMon.Obj);
                            if (cosm.ResultNumber != ResultEnum.Success)
                            {
                                MyMessageBox.ShowMessageBoxInfo("清空余额失败，请联系开发商！");
                                this.Dispose();
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
                MessageBox.Show(errorMessage, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ResultInfo<string> checkCpuEquipment = zq58Logical.CheckCpuEquipment();
            if (checkCpuEquipment.ResultNumber == ResultEnum.Success)
            {
                txtIDNo.Text = txtIDNoR.Text;
                txtCpuNo.Text = checkCpuEquipment.Obj;

                dateValided.Value = DateTime.Now.AddYears(3);
                cmbIDType.SelectedIndex = 0;
                cmbCpuVersion.SelectedIndex = 0;
                cmbIDType.SelectedIndex = 0;
                cmbCpuType.SelectedIndex = 0;

                tabReadUserInfo.Visible = true;
                tabReadCpu.Visible = false;
            }
            else
            {
                errorMessage = errorMessage + checkCpuEquipment.Describe;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        ///     返回度cpu卡的提示信息界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCpuUp_Click(object sender, EventArgs e)
        {
            tabReadCpu.Visible = true;
            tabReadUserInfo.Visible = false;
        }

        /// <summary>
        ///     返回用户卡信息输入界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click_1(object sender, EventArgs e)
        {
            tabSaveOver.Visible = false;
            tabReadUserInfo.Visible = true;
        }

        /// <summary>
        ///     保存用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click_Old(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();

                //先读一次用户卡
                ResultInfo<CpuAccount> tempa = zq58Logical.ReadAccountInfo();
                ResultInfo<BorrowBike> tempb = zq58Logical.ReadBorrowBikeInfo();
                if (tempa.ResultNumber == ResultEnum.Success)
                {
                    if ((Converts.Replace0Str(tempa.Obj.CpuCardStatus) == CpuCardStatus.Logout) || ((tempb.ResultNumber == ResultEnum.Success) && (tempb.Obj.EndAccountDate == "19000101")))
                    {
                        MyMessageBox.ShowMessageBoxInfo("卡片已注销，需要提交给应用开发商初始化，不能直接重新使用");
                        this.Dispose();
                        return;
                    }
                }

                //获取cpu卡用户信息
                var cpuAccount = new CpuAccount { CpuCardNo = txtCpuNo.Text, CpuCardVersion = cmbCpuVersion.Text };
                if (radioButton1.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                else if (radioButton2.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Logout;
                }
                else if (radioButton3.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Loss;
                }
                else
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                cpuAccount.CpuCardType = cmbCpuType.Text;
                cpuAccount.DisplayName = txtName.Text;
                cpuAccount.Fee = txtFee.Text;
                cpuAccount.Address = txtAddress.Text;
                cpuAccount.IdCardNo = txtIDNo.Text;
                cpuAccount.IdCardType = cmbIDType.Text;
                cpuAccount.Mobile = txtPhoneNo.Text;
                cpuAccount.Phone = txtTel.Text;
                cpuAccount.Sex = txtGender.Text;
                cpuAccount.Sex = cmbGender.Text;
                cpuAccount.Nation = txtNation.Text;
                cpuAccount.Nation = cmbNation.Text;

                this.Cursor = System.Windows.Forms.Cursors.WaitCursor; //出现忙碌光标

                ResultInfo<byte[]> writeAccount = zq58Logical.WriteAccountInfo(cpuAccount);
                if (writeAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<byte[]> borroBike =
                        zq58Logical.WriteBorrowBikeInfo(new BorrowBike
                        {
                            OpenAccountDate = Converts.ConvertDateToString(DateTime.Now),
                            EndAccountDate = Converts.ConvertDateToString(DateTime.Now.AddYears(3))
                        });
                    //这里是指为了写开户日期跟有效期
                    if (borroBike.ResultNumber == ResultEnum.Success)
                    {
                        Decimal money = Converts.ConvertToDecimal(tbInitialCzMoney.Text);
                        ResultInfo<byte[]> purseMoney = zq58Logical.PurseSave(money);
                        if (purseMoney.ResultNumber == ResultEnum.Success)
                        {
                            ResultInfo<Decimal> queryMoney = zq58Logical.QueryPurse();
                            if (queryMoney.ResultNumber == ResultEnum.Success)
                            {
                                lbTotalMoney.Text = queryMoney.Obj.ToString();



































                                //保存到数据库之前先查询用户卡的信息
                                ResultInfo<CpuAccount> newAccount = zq58Logical.ReadAccountInfo();
                                if (newAccount.ResultNumber == ResultEnum.Success)
                                {
                                    //将信息存入数据库

                                    try
                                    {
                                        var jshAccount = new jsh_account
                                        {
                                            addr1 = txtHomeAddress.Text,
                                            addr2 = txtAddress.Text,
                                            balance = Converts.ConvertToDecimal(queryMoney.Obj.ToString()),
                                            cardtype = newAccount.Obj.CpuCardType,
                                            closedate = dateValided.Value,
                                            cpuid = newAccount.Obj.CpuCardId,
                                            cpuno = newAccount.Obj.CpuCardNo,
                                            credit = 0,
                                            deposit = Converts.ConvertToDecimal(tbReceipt.Text),
                                            edate = dateValided.Value.ToString(),
                                            fzdw = txtIdGov.Text,
                                            khdate = DateTime.Now.ToString(),
                                            validate = dateValided.Value.ToString(),
                                            memo = txtMemo.Text,
                                            mobile = newAccount.Obj.Mobile,
                                            nation = newAccount.Obj.Nation,
                                            operator1 = "admin",
                                            operatorid = 0,
                                            password = txtPassword.Text
                                        };
                                        try
                                        {
                                            var reader = new StreamReader(userPicture.ImageLocation);
                                            Stream stream = reader.BaseStream;
                                            var imgs = new byte[stream.Length];
                                            stream.Read(imgs, 0, imgs.Length);
                                            jshAccount.photo = imgs;
                                        }
                                        catch (Exception)
                                        {
                                        }
                                        jshAccount.sdate = DateTime.Now.ToString();
                                        jshAccount.sex = newAccount.Obj.Sex;
                                        jshAccount.status = newAccount.Obj.CpuCardStatus;
                                        jshAccount.telephone = newAccount.Obj.Phone;
                                        jshAccount.username = newAccount.Obj.DisplayName;
                                        jshAccount.zzhm = newAccount.Obj.IdCardNo;
                                        jshAccount.zzlx = newAccount.Obj.IdCardType;

                                        ResultInfo<bool> openAccount = _account.OpenAccount(jshAccount,
                                            Converts.ConvertToDecimal(lbInitialMoney.Text),
                                            Converts.ConvertToDecimal(tbInitialCzMoney.Text),
                                            Converts.ConvertToDecimal(tbReceipt.Text), SystemInfo.OperateAddress,
                                            SystemInfo.OperateAddressId, SystemInfo.OperatePsamId);
                                        if (openAccount.ResultNumber == ResultEnum.Success)
                                        {
                                            if (openAccount.Obj)
                                            {
                                                MessageBox.Show(string.Format("保存成功！初始金额：{0}元，充值：{1}元，余额：{2}元!        ", lbInitialMoney.Text, lbCzMoney.Text, lbTotalMoney.Text));
                                                this.Dispose();
                                            }
                                        }
                                        else
                                        {
                                            //如果存入数据库没有成功，扣除刚刚充值到cpu卡的钱
                                            ResultInfo<byte[]> backMoney =
                                                zq58Logical.CostMoney(
                                                    Converts.ConvertToDecimal(tbInitialCzMoney.Text));
                                            if (backMoney.ResultNumber != ResultEnum.Success)
                                            {
                                                errorMessage = errorMessage + backMoney.Describe;
                                            }
                                            errorMessage = errorMessage + openAccount.Describe;
                                        }

                                        this.Cursor = System.Windows.Forms.Cursors.Default;
                                    }
                                    catch (Exception e1)
                                    {
                                        errorMessage = errorMessage + e1.Message;
                                    }
                                }
                                else
                                {
                                    errorMessage = errorMessage + newAccount.Describe;
                                }
                            }
                            else
                            {
                                lbTotalMoney.Text = queryMoney.Describe;

                            }
                        }
                        else
                        {
                            errorMessage = errorMessage + purseMoney.Describe;








                        }


                    }
                    else
                    {
                        errorMessage = errorMessage + borroBike.Describe;
                    }
                }
                else
                {
                    errorMessage = errorMessage + writeAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
                this.Cursor = System.Windows.Forms.Cursors.Default;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
            GC.Collect();
        }

        /// <summary>
        /// 开户保存新方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender,EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                
                //获取cpu卡用户信息
                var cpuAccount = new CpuAccount { CpuCardNo = txtCpuNo.Text, CpuCardVersion = cmbCpuVersion.Text };
                if (radioButton1.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                else if (radioButton2.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Logout;
                }
                else if (radioButton3.Checked)
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Loss;
                }
                else
                {
                    cpuAccount.CpuCardStatus = CpuCardStatus.Open;
                }
                cpuAccount.CpuCardType = cmbCpuType.Text;
                cpuAccount.DisplayName = txtName.Text;
                cpuAccount.Fee = txtFee.Text;
                cpuAccount.Address = txtAddress.Text;
                cpuAccount.IdCardNo = txtIDNo.Text;
                cpuAccount.IdCardType = cmbIDType.Text;
                cpuAccount.Mobile = txtPhoneNo.Text;
                cpuAccount.Phone = txtTel.Text;
                cpuAccount.Sex = txtGender.Text;
                cpuAccount.Sex = cmbGender.Text;
                cpuAccount.Nation = txtNation.Text;
                cpuAccount.Nation = cmbNation.Text;

                this.Cursor = System.Windows.Forms.Cursors.WaitCursor; //出现忙碌光标

                ResultInfo<decimal> openac = zq58Logical.OpenAccount(cpuAccount, Converts.ConvertToDecimal(tbInitialCzMoney.Text));
                if(openac.ResultNumber==ResultEnum.Success)
                {
                    //将信息存入数据库
                    try
                    {
                        var jshAccount = new jsh_account
                        {
                            addr1 = txtHomeAddress.Text,
                            addr2 = txtAddress.Text,
                            balance = openac.Obj,
                            cardtype = cpuAccount.CpuCardType,
                            closedate = dateValided.Value,
                            cpuid = cpuAccount.CpuCardNo,
                            cpuno = cpuAccount.CpuCardNo,
                            credit = 0,
                            deposit = Converts.ConvertToDecimal(tbReceipt.Text),
                            edate = dateValided.Value.ToString(),
                            fzdw = txtIdGov.Text,
                            khdate = DateTime.Now.ToString(),
                            validate = dateValided.Value.ToString(),
                            memo = txtMemo.Text,
                            mobile = cpuAccount.Mobile,
                            nation = cpuAccount.Nation,
                            operator1 = SystemInfo.OperateName,
                            operatorid = SystemInfo.OperateId,
                            password = txtPassword.Text
                        };
                        try
                        {
                            var reader = new StreamReader(userPicture.ImageLocation);
                            Stream stream = reader.BaseStream;
                            var imgs = new byte[stream.Length];
                            stream.Read(imgs, 0, imgs.Length);
                            jshAccount.photo = imgs;
                        }
                        catch (Exception)
                        {
                        }
                        jshAccount.sdate = DateTime.Now.ToString();
                        jshAccount.sex = cpuAccount.Sex;
                        jshAccount.status = cpuAccount.CpuCardStatus;
                        jshAccount.telephone = cpuAccount.Phone;
                        jshAccount.username = cpuAccount.DisplayName;
                        jshAccount.zzhm = cpuAccount.IdCardNo;
                        jshAccount.zzlx = cpuAccount.IdCardType;

                        ResultInfo<bool> openAccount = _account.OpenAccount(jshAccount,
                            Converts.ConvertToDecimal(lbInitialMoney.Text),
                            Converts.ConvertToDecimal(tbInitialCzMoney.Text),
                            Converts.ConvertToDecimal(tbReceipt.Text), SystemInfo.OperateAddress,
                            SystemInfo.OperateAddressId, SystemInfo.OperatePsamId);
                        if (openAccount.ResultNumber == ResultEnum.Success)
                        {
                            if (openAccount.Obj)
                            {
                                MessageBox.Show(string.Format("保存成功！初始金额：{0}元，充值：{1}元，余额：{2}元!        ", lbInitialMoney.Text, lbCzMoney.Text, lbTotalMoney.Text));
                                this.Dispose();
                            }
                        }
                        else
                        {
                            //如果存入数据库没有成功，扣除刚刚充值到cpu卡的钱
                            ResultInfo<byte[]> backMoney =
                                zq58Logical.CostMoney(
                                    Converts.ConvertToDecimal(tbInitialCzMoney.Text));
                            if (backMoney.ResultNumber != ResultEnum.Success)
                            {
                                errorMessage = errorMessage + backMoney.Describe;
                            }
                            errorMessage = errorMessage + openAccount.Describe;
                        }

                        this.Cursor = System.Windows.Forms.Cursors.Default;
                    }
                    catch (Exception e1)
                    {
                        errorMessage = errorMessage + e1.Message;
                    }
                }
                else
                {
                    errorMessage = errorMessage + openac.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
                this.Cursor = System.Windows.Forms.Cursors.Default;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
            GC.Collect();
        }

        /// <summary>
        ///     展示所有输入的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCpuNext_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            //if (txtValideDays.Text.Length > 7)
            //{
            //    errorMessage = "输入的有效天数不能超过7位数";
            //}
            if (string.IsNullOrEmpty(txtName.Text))
            {
                errorMessage = "用户名不能为空";
            }
            if (string.IsNullOrEmpty(txtPassword.Text) || (txtPassword.Text != txtPassword1.Text))
            {
                errorMessage = "密码为空或者两次输入密码不一致";
            }
            if (string.IsNullOrEmpty(txtIDNo.Text))
            {
                errorMessage = "证件号码不能为空";
            }
            if (string.IsNullOrEmpty(txtPhoneNo.Text) && string.IsNullOrEmpty(txtTel.Text))
            {
                errorMessage = "手机号或者座机号必须至少留一个";
            }
            var zq58Logical = new Zq58Logical();
            ResultInfo<Decimal> initialMoney = zq58Logical.QueryPurse();
            if (initialMoney.ResultNumber == ResultEnum.Success)
            {
                lbInitialMoney.Text = initialMoney.Obj.ToString();
            }
            else
            {
                errorMessage = initialMoney.Describe;
            }
            try
            {
                if (string.IsNullOrEmpty(errorMessage))
                {
                    //初始化用户信息显示
                    lbName.Text = txtName.Text;
                    lbSex.Text = txtGender.Text;
                    lbSex.Text = cmbGender.Text;
                    lbNation.Text = txtNation.Text;
                    lbNation.Text = cmbNation.Text;
                    lbValideDate.Text = dateValided.Value.ToString();
                    lbCpuVersion.Text = cmbCpuType.Text;
                    if (radioButton1.Checked)
                    {
                        lbStatus.Text = radioButton1.Text;
                    }
                    else if (radioButton2.Checked)
                    {
                        lbStatus.Text = radioButton2.Text;
                    }
                    else if (radioButton3.Checked)
                    {
                        lbStatus.Text = radioButton3.Text;
                    }
                    else
                    {
                        lbStatus.Text = radioButton1.Text;
                    }
                    lbCpuType.Text = cmbCpuType.Text;
                    lbFee.Text = txtFee.Text;
                    lbIdGov.Text = txtIdGov.Text;
                    lbIdNo.Text = txtIDNoR.Text;
                    lbIdType.Text = cmbIDType.Text;
                    lbMobile.Text = txtPhoneNo.Text;
                    lbTel.Text = txtTel.Text;
                    lbCzMoney.Text = tbInitialCzMoney.Text;
                    lbBorn.Text = dateBorn.Value.ToString();
                    lbHomeAddr.Text = txtHomeAddress.Text;
                    lbCurrentAddr.Text = txtAddress.Text;
                    lbCpuNo.Text = txtCpuNo.Text;
                    lbReceipt.Text = tbReceipt.Text;
                    lbMemo.Text = txtMemo.Text;
                    lbCpuVersion.Text = cmbCpuVersion.Text;
                    pictureBox1.ImageLocation = userPicture.ImageLocation;
                    tabReadUserInfo.Visible = false;
                    tabSaveOver.Visible = true;
                }
                else
                {
                    MyMessageBox.ShowMessageBoxError(errorMessage);
                }
            }
            catch (Exception e1)
            {
                MyMessageBox.ShowMessageBoxError(e1.Message);
            }
        }
        
		private void tabControlPanel1_Click(object sender, EventArgs e)
        {

        }

        private void labelX21_Click(object sender, EventArgs e)
        {

        }

        private void userPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "JPEG File(*.JPEG)|*.JPEG | BMP File(*.BMP)|*.BMP | JPG File(*.JPG)|*.JPG";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            openFileDialog.ShowHelp = true;
            openFileDialog.Title = "打开照片文件";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                userPicture.ImageLocation = openFileDialog.FileName;
            }
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            btnSave_Click(sender, e);
        }
    }
}